﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ErWeiMa</title>
    <style>
        html {
            height: 100%;
        }

        body {
            display: flex;
            width: 100%;
            height: 100%;
            justify-content: center;
            align-items: center;
        }
    </style>
</head>
<body>
    <div style="width:200px;height:200px;border:0px solid red" ><img id="aa" src="" style="width:200px;height:200px;" /></div>
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script>
        $(function () {
            downloadFile("https://localhost:5001/api/Wei/WeiM");
        })
        function downloadFile(url) {
            if (url == "" || url == null) { layer.msg("文件丢失或无文件下载"); return; }
            //设置下载名
            var splitString = url.split("/");
            console.log(splitString);
            //return;
            var fileName = "1233.jpg";
            try {
                var xhr;
                if (window.XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
                }
                else {
                    try {
                        xhr = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) {
                        layer.msg("下载失败");
                    }
                }
                //发起请求
                xhr.open("post", "/api/Wei/WeiM", true);
                //设置请求方式
                xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
                //设置二进制方式 接收数据
                xhr.responseType = "blob";
                xhr.onload = function (obj) {
                    if (xhr.status == 200 && xhr.readyState == 4) {
                        let content = xhr.response;
                        let blob = new Blob([content]);
                        console.log(blob);
                        if (blob.size <= 0) {
                            /*  layer.msg("失败或异常,下载内容为空");*/
                            alert("失败或异常,下载内容为空");
                            return;
                        }
                        //console.log(blob);
                        //组装A标签
                        //var a = document.createElement("a");
                        //a.download = fileName;
                        //a.style.display = "none";
                        var url = URL.createObjectURL(blob);
                        //a.href = url;
                        //console.log(url);
                        $("#aa").attr("src", url);
                        //document.body.appendChild(a);
                        //a.click();
                        //document.body.removeChild(a);
                    }
                }
                xhr.send();
            } catch (e) {
                /*    layer.msg("下载异常");*/
                alert("下载异常");
            }
           
        }
    </script>
</body>
</html>
